package mindmap.controller;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import mindmap.components.node.SerializableNode;
import mindmap.components.node.node;
import mindmap.operation.*;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

//新的
import javafx.scene.text.Text;

public class layoutController extends AnchorPane {

    //字体选择按钮
//    public MenuItem menuItem1;
//    public MenuItem menuItem2;
//    public MenuItem menuItem3;
//    public MenuItem menuItem4;
//    public MenuItem menuItem5;
    @FXML
    public Group group2;

    @FXML
    public Group group1;

    @FXML
    private AnchorPane ap;

    @FXML
    private Group baseGroup;

    @FXML
    private Button back;//撤回

    @FXML
    private ColorPicker backgroundColor;//背景颜色

    @FXML
    private Button bilateral;//双边布局

    @FXML
    private ColorPicker borderColor;//边框颜色

    @FXML
    private Button clear;

    @FXML
    private Button createFile;

    @FXML
    private Button exportPic;//导出图片

    @FXML
    private Tab files;

    @FXML
    private ColorPicker fillColor;//填充颜色

    @FXML
    private Button leftLayout;

    @FXML
    private Spinner<Double> width;//节点宽度

    @FXML
    private Button openFile;

    @FXML
    private Tab operations;

    @FXML
    private Spinner<Double> radius;

    @FXML
    private Slider radiusSlider;

    @FXML
    private Button recover;//恢复

    @FXML
    private Button rightLayout;

    @FXML
    private Button save;

    @FXML
    private Button saveAs;//另存为

    @FXML
    private Slider sizeChange;//字体大小

    @FXML
    private ColorPicker textColor;//字体颜色

    @FXML
    private TextField textContent;//文本内容

    @FXML
    private ComboBox textFamily;//字体样式

    @FXML
    private Spinner<Double> textSize;

    @FXML
    private CheckBox textUnderline;//字体下划线

    @FXML
    private CheckBox textWeight;//文本加粗

    @FXML
    private Spinner<Double> height;//节点高度

    @FXML
    private Spinner<Double> xLayout;

    @FXML
    private Spinner<Double> yLayout;

    //用于展示节点结构
    @FXML
    private ScrollPane showNode;

    public static TreeView<String> treeView ;

    //选中框
    public static Rectangle selectRect = new Rectangle();

    public node selectNode = null;
    public node root ;
    public FileOperation fileOperation;
    public SerializableNode serializableNode;
    private File openedFile;//当前保存的思维导图目标文件，下次保存直接覆盖该文件

    private static layoutController controller;
    LeftLayout left;
    RightLayout right;
    Bilateral bilateral1;

    public void initialize() {

        controller = this;
        selectRect.setWidth(104);
        selectRect.setHeight(46);
        //让矩形变透明
        selectRect.setFill(Color.TRANSPARENT);
        selectRect.setStroke(Color.BLUE);
        selectRect.setStrokeWidth(2);
        selectRect.setLayoutX(0);
        selectRect.setLayoutY(0);
        hideSelectRect();

        group2.getChildren().add(selectRect);

        root = new node(null,null, group1,group2,"right");
        root.setRoot(root);
        root.initLocation(root);
        treeView = new TreeView<>(root.getTreeItem());
        showNode.setPrefWidth(550);
        showNode.setContent(treeView);
        treeView.setPrefWidth(550);
        treeView.setEditable(true);
        treeView.setCellFactory(TextFieldTreeCell.forTreeView());
        fileOperation = new FileOperation();//导出保存文件
        fileOperation.setPane1(group1);
        fileOperation.setPane2(group2);//文件读取创建
        serializableNode = new SerializableNode();
        left = new LeftLayout(root);
        right = new RightLayout(root);
        bilateral1 = new Bilateral(root);

        back.setOnAction(event -> {
            unbindLabelAndTextField();
            selectNode = null;
            hideSelectRect();
            redoundo.undo();
        });

        recover.setOnAction(event -> {
            unbindLabelAndTextField();
            selectNode = null;
            hideSelectRect();
            redoundo.redo();
        });

        leftLayout.setOnAction(event -> {
            unbindLabelAndTextField();
            selectNode = null;
            hideSelectRect();
            if(root.getChildrenNode().isEmpty()) {
                root.arrangement = "left";
            }else {
                left.change();
                root.arrangement = "left";
            }
            redoundo.cleanAll();
        });
        rightLayout.setOnAction(event -> {
            unbindLabelAndTextField();
            selectNode = null;
            hideSelectRect();
            if(root.arrangement.equals("right")) {
                root.arrangement = "right";
            }else {
                right.change();
                root.arrangement = "right";
            }
            redoundo.cleanAll();
        });
        bilateral.setOnAction(event -> {
            unbindLabelAndTextField();
            selectNode = null;
            hideSelectRect();
            if(root.getChildrenNode().isEmpty()){
                root.arrangement = "bilateral";
            }else {
                bilateral1.change();
                root.arrangement = "bilateral";
            }
            redoundo.cleanAll();
        });

        //selectNode.label.textProperty().bindBidirectional(textContent.textProperty());

        //初始化字体大小右调节框的调节范围
        SpinnerValueFactory<Double> valueFactory = new SpinnerValueFactory.DoubleSpinnerValueFactory(5, 20, 12, 0.5);
        textSize.setValueFactory(valueFactory);
        textSize.getValueFactory().setValue(null);

        //初始化字体大小左调节框的调节范围
        sizeChange.setMin(5);
        sizeChange.setMax(20);

        //初始化弧度大小spinner的调节范围
        SpinnerValueFactory<Double> valueFactory2 = new SpinnerValueFactory.DoubleSpinnerValueFactory(0, 40, 12, 1);
        radius.setValueFactory(valueFactory2);
        radius.getValueFactory().setValue(null);

        //初始化弧度大小slider的调节范围
        radiusSlider.setMin(0);
        radiusSlider.setMax(40);

        //初始化节点高度大小spinner的调节范围
        SpinnerValueFactory<Double> valueFactory3 = new SpinnerValueFactory.DoubleSpinnerValueFactory(20, 100, 100, 1);
        height.setValueFactory(valueFactory3);
        height.getValueFactory().setValue(null);

        //初始化节点宽度大小spinner的调节范围
        SpinnerValueFactory<Double> valueFactory4 = new SpinnerValueFactory.DoubleSpinnerValueFactory(40, 140, 100, 1);
        width.setValueFactory(valueFactory4);
        width.getValueFactory().setValue(null);

        //初始化节点x位置spinner的调节范围
        SpinnerValueFactory<Double> valueFactory5 = new SpinnerValueFactory.DoubleSpinnerValueFactory(0, 440, 0, 1);
        xLayout.setValueFactory(valueFactory5);
        xLayout.getValueFactory().setValue(null);

        //初始化节点y位置spinner的调节范围
        SpinnerValueFactory<Double> valueFactory6 = new SpinnerValueFactory.DoubleSpinnerValueFactory(0, 440, 0, 1);
        yLayout.setValueFactory(valueFactory6);
        yLayout.getValueFactory().setValue(null);

        // 监听radius的变化并调用change函数
        radius.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if(selectNode != null) {
                    selectNode.ChangeRadius(selectNode.getisEdit());
                }
            }
        });
        //实现监听radius数值变化并同步更新radiusSlider的数值
        radius.valueProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue != null) {
                radiusSlider.setValue(newValue.doubleValue());
            }
        });

        //监听radiusSlider数值变化并同步更新radius的数值
        radiusSlider.valueProperty().addListener((obs, oldValue, newValue) -> {
            if (newValue != null) {
                radius.getValueFactory().setValue(newValue.doubleValue());
            }
        });

        // 监听height的变化并调用change函数
        height.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if(selectNode != null) {
                    selectNode.ChangeHeight(selectNode.getisEdit());
                }
            }
        });

        // 监听width的变化并调用change函数
        width.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if(selectNode != null) {
                    selectNode.ChangeWidth(selectNode.getisEdit());
                }
            }
        });

        // 监听xlayout的变化并调用change函数
        xLayout.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if(selectNode != null) {
                    selectNode.ChangeX(selectNode.getisEdit());
                }
            }
        });

        // 监听ylayout的变化并调用change函数
        yLayout.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if(selectNode != null) {
                    selectNode.ChangeY(selectNode.getisEdit());
                }
            }
        });

        //初始化textFamily
        textFamily.getItems().addAll("宋体", "黑体", "楷体","微软雅黑","Times New Roman");
        textFamily.getSelectionModel().select("宋体");

        //张世杰 监听textcolor的变化并调用change函数
        textColor.valueProperty().addListener((observable, oldValue, newValue) -> {
            //Color selectedColor = newValue;
            if(selectNode != null) {
                selectNode.ChangeFontColor(selectNode.getisEdit());
            }
        });

        //张世杰 监听fillcolor的比变化并调用change函数
        fillColor.valueProperty().addListener((observable, oldValue, newValue) -> {
            //Color fillColor = newValue;
            if(selectNode != null) {
                selectNode.ChangeFillColor(selectNode.getisEdit());
            }
        });

        //张世杰 监听BackgroundColor的比变化并调用change函数
        backgroundColor.valueProperty().addListener((observable, oldValue, newValue) -> {
            // 当背景颜色改变时，调用node类的changeBackgroundColor方法
            root.ChangeBackgroundColor();
        });

        //张世杰 监听bordercolor的比变化并调用change函数
        borderColor.valueProperty().addListener((observable, oldValue, newValue) -> {
            //Color borderColor = newValue;
            if(selectNode != null) {
                selectNode.ChangeBorderColor(selectNode.getisEdit());
            }
        });

        //张世杰 监听Backgroundcolor的比变化并调用change函数
        backgroundColor.valueProperty().addListener((observable, oldValue, newValue) -> {
            //Color backgroundColor = newValue;
            if(selectNode != null) {
                selectNode.ChangeBackgroundColor();
            }
        });

        //张世杰 监听text size的比变化并调用change函数
        sizeChange.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if(selectNode != null) {
                    ChangeTextSize();
                }
            }
        });

        //监听文本大小输入框的变化并调用change方法
        textSize.valueProperty().addListener((observable, oldValue, newValue) -> {
            if(selectNode != null)
            {
                ChangeTextSize2();

            }
        });

        //监听加粗按钮的变化并调用change方法
        textWeight.selectedProperty().addListener((obs, oldVal, newVal) -> {
            if(selectNode != null ) {
                    if (newVal) {
                        textWeight.setSelected(true);
                        selectNode.ChangeBold(selectNode.getisEdit());
                    } else {
                        textWeight.setSelected(false);
                        selectNode.ChangeBold(selectNode.getisEdit());
                    }

            }
        });


        //监听下划线按钮的变化并调用change方法
        textUnderline.selectedProperty().addListener((obs, oldVal, newVal) -> {
            if(selectNode != null) {
                    if (newVal) {
                        textUnderline.setSelected(true);
                        selectNode.ChangeUnderline(selectNode.getisEdit());
                    } else {
                        textUnderline.setSelected(false);
                        selectNode.ChangeUnderline(selectNode.getisEdit());
                    }
            }
        });


        //实现监听textSize数值变化并同步更新sizeChange的数值
        textSize.valueProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue != null) {
                sizeChange.setValue(newValue.doubleValue());
            }
        });

        //监听sizeChange数值变化并同步更新textSize的数值
        sizeChange.valueProperty().addListener((obs, oldValue, newValue) -> {
            if (newValue != null) {
                textSize.getValueFactory().setValue(newValue.doubleValue());
            }
        });


        // 监听文本框的输入变化，将文本内容同步到选中的节点
/*        selectNode.selectedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                // 当节点选中时，将label的文本与textContent绑定，并设置label的文本为textContent的值
                Bindings.bindBidirectional(selectNode.label.textProperty(), textContent.textProperty());
                selectNode.label.setText(textContent.getText());
            } else {
                // 当节点未选中时，解除绑定并清空label的文本
                Bindings.unbindBidirectional(selectNode.label.textProperty(), textContent.textProperty());
                selectNode.label.setText("");
            }
        });*/


        //字体选择触发对应方法
        textFamily.setOnAction(event -> {
            if(selectNode != null) {
                String selectedOption = textFamily.getSelectionModel().getSelectedItem().toString();
                if (selectedOption.equals("宋体")) {
                    setSimSun(selectNode.getisEdit());
                } else if (selectedOption.equals("黑体")) {
                    setSimHei(selectNode.getisEdit());
                } else if (selectedOption.equals("楷体")) {
                    setKaiTi(selectNode.getisEdit());
                } else if (selectedOption.equals("微软雅黑")) {
                    setMicrosoftYaHei(selectNode.getisEdit());
                } else if (selectedOption.equals("Times New Roman")) {
                    setTimesNewRoman(selectNode.getisEdit());
                }
            }
        });

//        //字体：宋体
//        menuItem1.setOnAction(new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent event) {
//                setSimSun(selectNode.getisEdit());
//            }
//        });
//
//        //黑体
//        menuItem2.setOnAction(new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent event) {
//                setSimHei(selectNode.getisEdit());
//            }
//        });
//
//        //楷体
//        menuItem3.setOnAction(new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent event) {
//                setKaiTi(selectNode.getisEdit());
//            }
//        });
//
//        //微软雅黑
//        menuItem4.setOnAction(new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent event) {
//                setMicrosoftYaHei(selectNode.getisEdit());
//            }
//        });
//
//        //Times New Roamn
//        menuItem5.setOnAction(new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent event) {
//                setTimesNewRoman(selectNode.getisEdit());
//            }
//        });
    }

    @FXML
    void clearCanvas(ActionEvent event) {

    }


    @FXML
    void redo(ActionEvent event) {

    }

    @FXML
    void setBGColor(ActionEvent event) {

    }

    @FXML
    void setBorderColor(ActionEvent event) {

    }

    @FXML
    void setFillColor(ActionEvent event) {

    }

    @FXML
    void setLeftLayout(ActionEvent event) {

    }

    @FXML
    void setRadius(ActionEvent event) {

    }

    @FXML
    void undo(ActionEvent event) {

    }

    @FXML
    void abcd(MouseEvent event) {
//        if (event.getButton() == MouseButton.PRIMARY) {
//            hideTextArea();
//        }
    }


    //当鼠标点击节点外面的地方时触发
    @FXML
    void canvasPress(MouseEvent event) {
        if(event.getButton() == MouseButton.PRIMARY){
            hideSelectRect();
            if(selectNode != null) {
                //解除文本框绑定
                unbindLabelAndTextField();
                this.selectNode = null;
                //清空文字加粗和下划线
                textWeight.setSelected(false);
                textUnderline.setSelected(false);
                borderColor.setValue(Color.TRANSPARENT);
                fillColor.setValue(Color.TRANSPARENT);
                //textContent.clear();
                CheckBlank();
                textSize.getValueFactory().setValue(null);
                sizeChange.valueProperty().setValue(null);
                radius.getValueFactory().setValue(null);
                radiusSlider.valueProperty().setValue(null);
                width.getValueFactory().setValue(null);
                height.getValueFactory().setValue(null);
                xLayout.getValueFactory().setValue(null);
                yLayout.getValueFactory().setValue(null);
            }

        }
    }

    //导出图片
    @FXML
    void exportImg(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("保存图片文件");
        fileChooser.setInitialDirectory(new File("."));
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.JPG"),
                new FileChooser.ExtensionFilter("图片类型", "*.png", "*.PNG")
        );
        File file = fileChooser.showSaveDialog(new Stage());
        if(file != null) {
            //将思维导图界面截图保存到文件中
            selectRect.setVisible(false);//隐藏选择框
            WritableImage image = baseGroup.snapshot(new SnapshotParameters(), null);
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        selectRect.setVisible(true);//回显
    }

    //新建文件
    @FXML
    void createfile(ActionEvent event) {
        save();
        group1.getChildren().remove(root);
        group2.getChildren().remove(root);
        root.minHeightNode = null;
        root.maxHeightNode = null;
        node.rightChildrenNode.clear();
        node.leftChildrenNode.clear();
        node.deleteList.clear();
        node.rightMaxHeight = null;
        node.rightMinHeight = null;
        node.leftMinHeight = null;
        node.leftMaxHeight = null;
        if(root.getChildrenNode().size() > 0) {
            root.deleteNode();
        }
        node newmap = new node(null,null,group1,group2,"right");
        newmap.setRoot(newmap);
        root = newmap;
        left.setRoot(newmap);
        right.setRoot(newmap);
        bilateral1.setRoot(newmap);
        redoundo.cleanAll();
    }
    //保存为文件
    @FXML
    void saveAsFile(ActionEvent event) {
        if(openedFile == null) {
            FileChooser fc = new FileChooser();
            fc.setTitle("保存文件");
            fc.setInitialDirectory(new File("."));
            fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("思维导图类型", "*.mpp", "*.MPP"));
            openedFile = fc.showSaveDialog(new Stage());
        }
        if(openedFile != null) {
            try {
                //保存思维导图文件
                fileOperation.writeFile(root, openedFile.getPath());
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setContentText("保存成功！");
                alert.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //另存为文件
    @FXML
    void saveAsOther(ActionEvent event) {
        save();
    }

    public void save(){
        FileChooser fc = new FileChooser();
        fc.setTitle("文件另存为");
        fc.setInitialDirectory(new File("."));
        fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("思维导图类型", "*.mpp", "*.MPP"));
        File file = fc.showSaveDialog(new Stage());
        if(file != null) {
            try {
                //保存思维导图文件
                fileOperation.writeFile(root, file.getPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//打开读取思维导图文件
    @FXML
    void readFile(ActionEvent event) {
        FileChooser fc = new FileChooser();
        fc.setTitle("读取文件");
        fc.setInitialDirectory(new File("."));
        //读取文件只能读取思维导图类型的文件
        fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("思维导图类型", "*.mpp", "*.MPP"));
        File file = fc.showOpenDialog(new Stage());
        if(file != null) {
            openedFile = file;
            try {
                group1.getChildren().remove(root);
                group2.getChildren().remove(root);
                root.minHeightNode = null;
                root.maxHeightNode = null;
                node.rightChildrenNode.clear();
                node.leftChildrenNode.clear();
                node.deleteList.clear();
                node.rightMaxHeight = null;
                node.rightMinHeight = null;
                node.leftMinHeight = null;
                node.leftMaxHeight = null;
                if(root.getChildrenNode().size() > 0) {
                    root.deleteNode();
                }
                node newmap = fileOperation.readFile(file.getPath());
                root = newmap;
                left.setRoot(newmap);
                right.setRoot(newmap);
                bilateral1.setRoot(newmap);
                redoundo.cleanAll();


            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }


    //隐藏选中框
    public void hideSelectRect(){
       // selectRect.setDisable(true);
        selectRect.setVisible(false);
    }

    public void showSelectRect(){
        selectRect.setDisable(false);
        selectRect.setVisible(true);
    }

    //当节点被选中时调用 鼠标
    public void selectNode(node node){
        this.selectNode = node;
        selectRect.setLayoutX(node.getLayoutX()-2);
        selectRect.setLayoutY(node.getLayoutY()-3);
        //同步各项节点属性
        checklabel();
        node.CheckNode();
        //读取同步文字加粗
        if(isTextWeight())
        {
            textWeight.setSelected(true);

        }else
        {
            textWeight.setSelected(false);

        }
        //读取同步文字下划线
        if(isTextUnderline())
        {
            textUnderline.setSelected(true);

        }else
        {
                textUnderline.setSelected(false);

        }

        //同步文本size右调节框
        double size = selectNode.label.getFont().getSize();
        textSize.getValueFactory().setValue(size);
        //同步文本radius调节框
        double radiusvalue = selectNode.getRectangle().getArcHeight();
        radius.getValueFactory().setValue(radiusvalue);
        //同步文本height调节框
        double heightvalue = selectRect.getHeight();
        height.getValueFactory().setValue(heightvalue);
        //同步文本width调节框
        double widthvalue = selectRect.getWidth();
        width.getValueFactory().setValue(widthvalue);
        double x = selectNode.getLayoutX();
        xLayout.getValueFactory().setValue(x);
        double y = selectNode.getLayoutY();
        yLayout.getValueFactory().setValue(y);
        //绑定文本框
        bindLabelAndTextField();
    }

    //设置画布颜色
    private Color BackgroundColor;
    public  void setBackgroundColor(Color color)
    {
        BackgroundColor = color;
        ap.setStyle("-fx-background-color: "+toHex(BackgroundColor)+";");

    }

    //改变字体大小
    public void ChangeTextSize()
    {
        double size = sizeChange.getValue();
        //selectNode.label.setStyle("-fx-font-size: " + size + "pt;");
        String FontFamily = layoutController.getInstance().selectNode.label.getFont().getFamily();
        FontWeight fontWeight = layoutController.getInstance().selectNode.label.getFont().getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        selectNode.label.setFont(Font.font(FontFamily,fontWeight,size));
    }

    public void ChangeTextSize2()
    {
        double size = textSize.getValue();
        //selectNode.label.setStyle("-fx-font-size:" + size + "pt;");
        String FontFamily = layoutController.getInstance().selectNode.label.getFont().getFamily();
        FontWeight fontWeight = layoutController.getInstance().selectNode.label.getFont().getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        selectNode.label.setFont(Font.font(FontFamily,fontWeight,size));
    }

    //字体

    //宋体
    public void setSimSun(boolean isEdit)
    {
        double fontSize = layoutController.getInstance().selectNode.label.getFont().getSize();
        FontWeight fontWeight = layoutController.getInstance().selectNode.label.getFont().getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        selectNode.label.setFont(Font.font("SimSun",fontWeight,fontSize));

    }

    //黑体
    public void setSimHei(boolean isEdit)
    {
        double fontSize = layoutController.getInstance().selectNode.label.getFont().getSize();
        FontWeight fontWeight = layoutController.getInstance().selectNode.label.getFont().getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        selectNode.label.setFont(Font.font("SimHei",fontWeight,fontSize));

    }

    //楷体
    public void setKaiTi(boolean isEdit)
    {
        double fontSize = layoutController.getInstance().selectNode.label.getFont().getSize();
        FontWeight fontWeight = layoutController.getInstance().selectNode.label.getFont().getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        selectNode.label.setFont(Font.font("KaiTi",fontWeight,fontSize));

    }

    //微软雅黑
    public void setMicrosoftYaHei(boolean isEdit)
    {
        double fontSize = layoutController.getInstance().selectNode.label.getFont().getSize();
        FontWeight fontWeight = layoutController.getInstance().selectNode.label.getFont().getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        selectNode.label.setFont(Font.font("Microsoft YaHei",fontWeight,fontSize));

    }

    //Times New Roman
    public void setTimesNewRoman(boolean isEdit)
    {
        double fontSize = layoutController.getInstance().selectNode.label.getFont().getSize();
        FontWeight fontWeight = layoutController.getInstance().selectNode.label.getFont().getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        selectNode.label.setFont(Font.font("Times New Roman",fontWeight,fontSize));

    }

    //张世杰 将颜色转换为16进制表示
    private String toHex(Color color) {
        String hex = String.format("#%02X%02X%02X", (int)(color.getRed()*255), (int)(color.getGreen()*255), (int)(color.getBlue()*255));
        return hex;
    }

    //返回选定文字颜色
    public Color getTextColor()
    {
        return textColor.getValue();
    }

    //返回选定填充颜色
    public Color getFillColor()
    {
        return fillColor.getValue();
    }

    //返回画布颜色
    public Color getBackgroundColor()
    {
        return backgroundColor.getValue();
    }

    //返回边框颜色
    public Color getborderColor()
    {
        return borderColor.getValue();
    }
    //返回边框弧度
    public double getRadius()
    {
        return radius.getValue();
    }
    //返回是否加粗
    public boolean getBold() {

        return textWeight.isSelected();
    }
    //返回是否下划线
    public boolean getUnderline() {
//        System.out.println(textUnderline.isSelected());
        return textUnderline.isSelected();
    }
    //返回节点高度
    public double getHeightValue()
    {
        return height.getValue();
    }
    //返回节点宽度
    public double getWidthValue()
    {
        return width.getValue();
    }
    //返回节点x值
    public double getXValue()
    {
        return xLayout.getValue();
    }
    //返回节点y值
    public double getYValue()
    {
        return yLayout.getValue();
    }

    //读取label的各项属性
    public void checklabel()
    {
        if (selectNode.getisEdit()) {
            double fontSize = selectNode.label.getFont().getSize();
            Color textcolor = (Color) selectNode.label.getTextFill();
            Font font = selectNode.label.getFont();

            //check字体并显示
            if (font.getName().equals("SimSun")) {
                textFamily.getSelectionModel().select("宋体");
            } else if (font.getName().equals("KaiTi")) {
                textFamily.getSelectionModel().select("楷体");
            } else if (font.getName().equals("Microsoft YaHei")) {
                textFamily.getSelectionModel().select("微软雅黑");
            } else if (font.getName().equals("SimHei")) {
                textFamily.getSelectionModel().select("黑体");
            } else if (font.getName().equals("Times New Roman")) {
                textFamily.getSelectionModel().select("Times New Roman");
            }else {
                textFamily.getSelectionModel().select("宋体");
            }

            //字体颜色显示
            textColor.setStyle("-fx-color-label-visible: false; -fx-display-color: red;");


        }

    }


    //加粗label文本
    public void ChangeTextWeight(boolean isBold)
    {
        Font font = selectNode.label.getFont();
        Font newFont;
        if(isBold) {
            newFont = Font.font(font.getFamily(), FontWeight.EXTRA_BOLD, font.getSize());
        }
        else  {
            newFont = Font.font(font.getFamily(), FontWeight.NORMAL, font.getSize());
        }
        selectNode.label.setFont(newFont);
    }


//    // 取消加粗label文本
//    public void unChangeTextWeight() {
//        Font font = selectNode.label.getFont();
//        Font newFont = Font.font(font.getFamily(), FontWeight.NORMAL, font.getSize());
//        selectNode.label.setFont(newFont);
//    }

    //添加下划线
    public void ChangeTextUnderline(boolean isUnderline)
    {
        if (isUnderline) {
            selectNode.label.setStyle("-fx-underline: true;");
        }
        else {
            selectNode.label.setStyle("-fx-underline: false;");
        }
    }

//    //取消下划线
//    public void unChangeTextUnderline()
//    {
//        selectNode.label.setStyle("-fx-underline: false;");
//    }


    //绑定文本输入框
    private void bindLabelAndTextField() {
        textContent.setText(selectNode.label.getText());
        selectNode.label.textProperty().bindBidirectional(textContent.textProperty());
    }

    public void unbindLabelAndTextField() {
        if(selectNode != null) {
            selectNode.label.textProperty().unbindBidirectional(textContent.textProperty());
            textContent.textProperty().unbindBidirectional(selectNode.label.textProperty());
            textContent.setText("");
            //Bindings.unbindBidirectional(selectNode.textProperty(), selectNode.label.textProperty());
        }
    }

    public void CheckBlank()
    {
        textFamily.getSelectionModel().select("宋体");
    }

    //判断字体是否加粗
/*    public boolean isTextWeight()
    {
        Font font = selectNode.label.getFont();
        FontWeight fontWeight = font.getWeight();
        return fontWeight.equals(FontWeight.BOLD);
    }*/
/*    public boolean isTextWeight()
    {
        Font font = selectNode.label.getFont();
        FontWeight fontWeight = FontWeight.findByWeight((int) font.getStyle());
        return fontWeight.equals(FontWeight.BOLD);
    }*/

    //判断字体是否加粗
    public boolean isTextWeight()
    {
        Font font = selectNode.label.getFont();
        FontWeight fontWeight = font.getStyle().equals("Bold") ? FontWeight.EXTRA_BOLD : FontWeight.NORMAL;
        return fontWeight.equals(FontWeight.EXTRA_BOLD);
    }


    //判断文本是否加了下划线
    public boolean isTextUnderline()
    {
        String style = selectNode.label.getStyle();
        return style.contains("-fx-underline: true;") || style.contains("-fx-underline:true;");
    }

    public void setwhile()
    {
        borderColor.setValue(Color.TRANSPARENT);
        fillColor.setValue(Color.TRANSPARENT);
    }
    public void setborderColor1(Color borderColor1)
    {
        borderColor.setValue(borderColor1);
    }

    public void setfillColor1(Color fillColor1)
    {
        fillColor.setValue(fillColor1);
    }

    //获取layoutController本类
    public static layoutController getInstance(){
            return controller;
    }

}
